/**数据储存的方法，其中的key作为公共数据**/
import Const from '@/config'
const DATA = Const.DATA
function isLocalStorageUsable() {
	const localStorageTestKey = '__localStorage_support_test'
	const localStorageTestValue = 'test'
	let isSupport = false
	try {
		localStorage.setItem(localStorageTestKey, localStorageTestValue)
		if (localStorage.getItem(localStorageTestKey) === localStorageTestValue) {
			isSupport = true
		}
		localStorage.removeItem(localStorageTestKey)
		return isSupport
	} catch (e) {
		if (e.name === 'QuotaExceededError' || e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
			console.warn('localStorage 存储已达上限！')
		} else {
			console.warn('当前浏览器不支持 localStorage！')
		}
		return isSupport
	}
}

function normalizeKey(key) {
	if (typeof key !== 'string') {
		console.warn(`${key} used as a key, but it is not a string.`)
		key = String(key)
	}
	return key
}

function serialize(value, callback) {
	try {
		const valueString = JSON.stringify(value)
		callback(null, valueString)
	} catch (e) {
		callback(e)
	}
}
class StorageHandler {
	ready() {
		if (this.isSupport == null) {
			this.isSupport = isLocalStorageUsable()
		}
		return this.isSupport
	}

	setItem(key, value) {
		key = normalizeKey(key)
		if (this.ready()) {
			if (value === undefined) {
				value = null
			}
			serialize(value, (error, valueString) => {
				if (error) {
					return
				}
				try {
					// 可能会因超出最大存储空间，存储失败。
					const FINAL_KEY = `${DATA.KEY_PREFIX}${key}`
					localStorage.setItem(FINAL_KEY, valueString)
				} catch (e) {
					console.log('e', e)
				}
			})
		}
	}
	getItem(key) {
		key = normalizeKey(key)
		const FINAL_KEY = `${DATA.KEY_PREFIX}${key}`
		return JSON.parse(localStorage.getItem(FINAL_KEY))
	}
	clearItem(key) {
		const FINAL_KEY = `${DATA.KEY_PREFIX}${key}`
		return localStorage.removeItem(FINAL_KEY)
	}

	setToken(value) {
		this.setItem(DATA.KEY_TOKEN, value)
	}
	getToken() {
		return this.getItem(DATA.KEY_TOKEN)
	}
	clearToken() {
		return this.clearItem(DATA.KEY_TOKEN)
	}

	setLanguage(value) {
		this.setItem(DATA.KEY_LANGUAGE, value)
	}
	getLanguage() {
		return this.getItem(DATA.KEY_LANGUAGE)
	}
	setUser(value) {
		this.setItem(DATA.KEY_USER, value)
	}
	getUser() {
		return this.getItem(DATA.KEY_USER)
	}
	clearUser() {
		return this.clearItem(DATA.KEY_USER)
	}
	setCarType(value) {
		this.setItem(DATA.KEY_CAR_TYPE, value)
	}
	getCarType() {
		return this.getItem(DATA.KEY_CAR_TYPE)
	}

	setPaypalScriptSrc(value) {
		this.setItem(DATA.KEY_PAYPAL_SCRIPT_SRC, value)
	}
	getPaypalScriptSrc() {
		return this.getItem(DATA.KEY_PAYPAL_SCRIPT_SRC)
	}
	setPreOrderSn(value) {
		this.setItem(DATA.KEY_PRE_ORDER_SN, value)
	}
	getPreOrderSn() {
		return this.getItem(DATA.KEY_PRE_ORDER_SN)
	}
	// 设置国家
	setCountry(value) {
		this.setItem(DATA.KEY_COUNTRY, value)
	}
	getCountry() {
		return this.getItem(DATA.KEY_COUNTRY)
	}
	// 设置区域 方便获取区号
	setArea(value) {
		this.setItem(DATA.KEY_AREA, value)
	}
	getArea() {
		return this.getItem(DATA.KEY_AREA)
	}
	// 用户中心页面切换
	setUserCenterPage(value) {
		this.setItem(DATA.USER_CENTER_INDEX, value)
	}
	getUserCenterPage() {
		return this.getItem(DATA.USER_CENTER_INDEX)
	}
	clearUserCenterPage() {
		return this.clearItem(DATA.USER_CENTER_INDEX)
	}

	setPlayStatus(value) {
		this.setItem(DATA.PLAY_STATUS, value)
	}
	getPlayStatus() {
		return this.getItem(DATA.PLAY_STATUS)
	}
}

export default new StorageHandler()
